package com.kafka.consumer;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

// 消息消费者
@Component
@Slf4j
public class KafkaConsumer {



    @Resource
    private ConsumerFactory<String, String> consumerFactory;

    @Resource
    private KafkaListenerEndpointRegistry registry;

    // topicPartitions = {@TopicPartition(topic = "ChatMsg", partitionOffsets = @PartitionOffset(partition = "0", initialOffset = "0")),
    @KafkaListener(id = "log", containerFactory = "containerFactory", topics = {"logMessage"})
    public void LogMessage(ConsumerRecords<String, String> records, Acknowledgment ack) {
        for (ConsumerRecord<?, ?> record : records) {
            System.out.println("*: " + record.value() + "  key: " + record.key() + " offset: " + record.offset() + " partition: " + record.partition());
            ack.acknowledge();
        }
    }

    @KafkaListener(id = "sms", containerFactory = "containerFactory", topics = {"smsMessage"})
    public void SmsMessage(ConsumerRecords<String, String> records, Acknowledgment ack)  {
        for (ConsumerRecord<?, ?> record : records) {

        }
    }
}
